import selenium
from selenium import webdriver
import pathlib
import time

from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

from autotest import AutoUtil

# 基本信息
# 视频存放路径
catalog_mp4 = r"C:\Users\Administrator\Desktop\视频发布"
# 视频描述
describe = "裸眼3D看蜘蛛侠 #搞笑 #电影 #视觉震撼"
time.sleep(10)
driver = AutoUtil.get_driver()

path = pathlib.Path(catalog_mp4)

# 视频地址获取
path_mp4 = ""
for i in path.iterdir():
    if (".mp4" in str(i)):
        path_mp4 = str(i);
        break;

if (path_mp4 != ""):
    print("检查到视频路径：" + path_mp4)
else:
    print("未检查到视频路径，程序终止！")
    exit()

# 封面地址获取
path_cover = ""
for i in path.iterdir():
    if (".png" in str(i) or ".jpg" in str(i)):
        path_cover = str(i);
        break;

if (path_cover != ""):
    print("检查到封面路径：" + path_cover)
else:
    print("未检查到封面路径，程序终止！")
    exit()


def publish_kuaishou():
    '''
     作用：发布快手视频
    '''

    # 进入创作者页面，并上传视频
    driver.get("https://cp.kuaishou.com/article/publish/video?origin=www.kuaishou.com")
    time.sleep(3)
    driver.find_element(By.XPATH,'//input[@type="file"]').send_keys(path_mp4)

    # 等待视频上传完成
    while True:
        time.sleep(3)
        try:
            driver.find_element(By.XPATH,'//*[contains(text(),"上传成功")]')
            break;
        except Exception as e:
            print("视频还在上传中···")

    print("视频已上传完成！")

    # 添加封面
    time.sleep(2)
    driver.find_element(By.XPATH,'//button//*[contains(text(),"编辑封面")]').click()
    # 进入iframe框架
    driver.switch_to.frame(driver.find_element(By.XPATH,'//iframe'))
    time.sleep(1)
    driver.find_element(By.XPATH,'//input[@type="file"]').send_keys(path_cover)
    time.sleep(10)
    driver.find_element(By.XPATH,'//button[text()="确定"]').click()
    # 退出默认框架
    driver.switch_to_default_content()

    # 切换常规视频
    time.sleep(2)
    driver.find_element(By.XPATH,'//*[contains(text(),"去上传常规视频")]').click()

    time.sleep(3)
    # 输入视频描述
    driver.find_element(By.XPATH,'//*[@placeholder="添加合适的话题和描述，作品能获得更多推荐～"]').send_keys(describe)

    # 选择分类
    driver.find_element(By.XPATH,'//*[@placeholder="请选择"]').click()
    time.sleep(2)
    driver.find_element(By.XPATH,'//*[text()="影视"]').click()
    time.sleep(1)

    # 人工进行检查并发布
    # time.sleep(3)
    # # 点击发布
    # driver.find_element(By.XPATH,'//*[text()="发布"]').click()


# 开始执行视频发布
publish_kuaishou()